package jlxy.ise.eroad.adapter;

import android.app.Activity;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.ImageLoaderConfiguration;

import java.util.List;

import jlxy.ise.eroad.R;
import jlxy.ise.eroad.bean.PackageInfo;


/**
 * 订单信息
 */
public class OrderAdapter extends RecyclerView.Adapter<OrderAdapter.ViewHolder> {

    private List<PackageInfo.DataBean> mInfoList;

    private Activity mAct;

    OnItemClick mOnItemClick;

    private static ImageLoader imageLoader = ImageLoader.getInstance();

    /**
     * 回调接口，点击了哪个item
     */
    public interface OnItemClick {
        void OnClickItem(PackageInfo.DataBean mInfo);

        void OnLongClickItem(PackageInfo.DataBean mInfo);
    }


    public OrderAdapter(List<PackageInfo.DataBean> list, Activity act, OnItemClick onItemClick) {
        this.mInfoList = list;
        this.mAct = act;
        mOnItemClick = onItemClick;
        imageLoader.init(ImageLoaderConfiguration.createDefault(mAct));
    }


    @Override
    public ViewHolder onCreateViewHolder(final ViewGroup viewGroup, int i) {
        View v = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.listitem_package, viewGroup, false);
        return new ViewHolder(v);
    }

    @Override
    public void onBindViewHolder(final ViewHolder viewHolder, int i) {
        final PackageInfo.DataBean mInfo = mInfoList.get(i);


        String pkgInfoStr = "运单号:" + mInfo.getId() + "\t\t" + "价格:" + mInfo.getPrice();
        String pkgInfoOtherStr = "目的地:" + mInfo.getEprovince() + mInfo.getEcity() + mInfo.getEdistrict() + mInfo.getEaddr()
                + "\n" + "发货时间:" + mInfo.getStime()+ "\n" + "货物内容:" + mInfo.getInfo() ;

        viewHolder.pkgInfo.setText(pkgInfoStr);
        viewHolder.pkgOtherInfo.setText(pkgInfoOtherStr);

        viewHolder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (mOnItemClick != null) {
                    mOnItemClick.OnClickItem(mInfo);
                }
            }
        });

        viewHolder.itemView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mOnItemClick != null) {
                    mOnItemClick.OnLongClickItem(mInfo);
                }
                return true;
            }
        });


    }

    @Override
    public int getItemCount() {
        return mInfoList == null ? 0 : mInfoList.size();
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {

        public TextView pkgInfo;
        public TextView pkgOtherInfo;

        public ViewHolder(View itemView) {
            super(itemView);
            pkgInfo = (TextView) itemView.findViewById(R.id.pkg_info);
            pkgOtherInfo = (TextView) itemView.findViewById(R.id.pkg_other_info);
        }

    }
}
